const BaseIpcHandler = require('../BaseIpcHandler');
const { RemoteScriptInvoker } = require('../../remote/remoteScriptInvoker');

class RemoteSystemInfoHandler extends BaseIpcHandler {
    async handle(event, params) {
        // 解析参数
        // console.log(`>> RemoteSystemInfoHandler params = `, params);
        const { remoteId } = params;

        // 业务逻辑
        let result = null;
        try {
            let invoker = new RemoteScriptInvoker(this.app, remoteId);
            let system_info = await invoker.getSystemInfo();
            if (!system_info) {
                result = { code: -1, msg: '获取系统信息失败！' };
            } else {
                result = { code: 0, msg: JSON.stringify(system_info) };
            }
        } catch (err) {
            console.error(`Get remote${remoteId} system info Failed!, Error = `, err);
            result = { code: -1, msg: err };
        }

        // 返回结果
        // console.log(`RemoteSystemInfoHandler result = `, result);
        if (event.from === 'localserver') {
            event.resp.send(result);
        } else {
            return result;
        }
    }
}

module.exports = RemoteSystemInfoHandler;
